<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class collegian_activity  extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
                public $URL;
                public function __construct() {
                    session_start();
                    parent::__construct();
                    $this->URL = $this->uri->uri_to_assoc();
                    $this->authen_model->CheckPermission();
                    $this->load->model("collegian_model");
                    $this->load->model("collegian_activity_model");
                }
	public function index()
	{
                      $this->my_activity();
	}
        
                public function report(){
                        $data["activity_y"] = $this->collegian_activity_model->get_report_activity_y($_SESSION["id"]);
                        $data["activity_n"] = $this->collegian_activity_model->get_report_activity_n($_SESSION["id"]);
                        $this->templates
                                ->setContent($this->load->view("activity/collegian_activity/report/collegian_report",$data,true))
                                ->setTitle("ระบบออกรายงานการเข้าร่วมกิจกรรมนักศึกษา")
                                ->rander();
                }
        
        
                public function view_activity_detail(){
                    if($this->URL["ID"] != ""){
                                $CI =& get_instance();
                                $this->load->model("activity_model");
                                $this->load->model("major_model");
                                $this->load->model("teacher_model");
                                $data["activity_datay"] = $this->activity_model->get_activity(mysql_escape_string($this->URL["ID"]));
                                $data["CI"] = $CI;
                                $data["act_id"] = mysql_escape_string($this->URL["ID"]);
                                $data["target"] = mysql_escape_string($this->URL["target"]);
                                $this->templates
                                        ->appendJs("activity_collegian.js")
                                        ->setContent($this->load->view("activity/collegian_activity/view_activity_detail_by_id",$data,true))
                                        ->setTitle("ข้อมูลรายละเอียดกิจกรรม")
                                        ->rander();
                    }
                }

                public function view_activitys_search(){
                    $data["majors"] = $this->collegian_model->get_majors();
                        $this->templates
                                ->appendJs("admin.js")
                                ->appendJs("activity_collegian.js")
                                ->setContent($this->load->view("activity/collegian_activity/view_activitys",$data,true))
                                ->setTitle("ข้อมูลกิจกรรม")
                                ->rander();
                }
                
                /* แสดงข้อมูลกิจกรรมที่ต้องเข้าร่วม */
                public function my_activity(){
                    
                        $data["collegian_activity_yes"] = $this->collegian_activity_model->get_collegian_activity($_SESSION["id"],"Y");
                        $data["collegian_activity_no"] = $this->collegian_activity_model->get_collegian_activity($_SESSION["id"],"N");
                        $this->templates
                               // ->appendJs("activity.js")
                                ->appendJs("activity_collegian.js")
                                ->setContent($this->load->view("activity/collegian_activity/my_activity",$data,true))
                                ->setTitle("ข้อมูลกิจกรรมนักศึกษา")
                                ->rander();
                }
                
                public function search_activity(){
                    $this->load->model("activity_model");
                    $data["activitys_datay"] = $this->activity_model->search_activity_event(
                            $_POST
                    );
                    $data["count"] = $this->db_model->getCount();
                    $this->load->view("activity/collegian_activity/collegian_search_activity_table",$data);
                }
                
                public function view_activity(){
                    if($this->URL["ID"] != ""){
                                $CI =& get_instance();
                                $this->load->model("activity_model");
                                $this->load->model("major_model");
                                $this->load->model("teacher_model");
                                $data["activity_datay"] = $this->activity_model->get_activity(mysql_escape_string($this->URL["ID"]));
                                $data["CI"] = $CI;
                                $data["act_id"] = mysql_escape_string($this->URL["ID"]);
                                $this->templates
                                        ->appendJs("activity_collegian.js")
                                        ->setContent($this->load->view("activity/collegian_activity/view_activity_detail_2",$data,true))
                                        ->setTitle("ข้อมูลรายละเอียดกิจกรรม")
                                        ->rander();
                    }
                }
                
                public function test(){
                    $this->load->model("activity_model");
                    $data =  $this->activity_model->get_activity_groups_relation_by_activity_id(1);
                  
                print_r(   $data->row() );
                }
                
                public function unset_collegian_activity_confirm(){
                    $this->load->model("activity_model");
                    if(isset($_POST["collegians_of_activity_id"])){
                        $sql = "DELETE FROM collegians_of_activity WHERE collegians_of_activity_id = '$_POST[collegians_of_activity_id]' ";
                        $this->db->query($sql);
                    }
                }
                
                public function confirm_code(){
                    if(isset($_POST["code"],$_POST["act_id"])){
                            $user = $this->db->select("*")->from("activity_events")
                                    ->join("activity_groups_relation","activity_groups_relation.ref_act_id = activity_events.act_id","inner")
                                    ->join("group_major_relation","group_major_relation.ref_activity_groups_relation_id = activity_groups_relation.activity_groups_relation_id","inner")
                                    ->join("collegians_of_activity" , "collegians_of_activity.ref_group_major_relation_id = group_major_relation.group_major_relation_id","inner")
                                    ->where("ref_collegian_id",$_SESSION["id"])
                                    ->where("activity_events.act_id",$_POST["act_id"] )
                                    ->limit(1)->get();
                            $id = $user->row(0)->collegians_of_activity_id;
                            
                            $sql = "SELECT *  FROM collegians_of_activity  WHERE  collegians_of_activity_id = '{$id}'  AND  MID(code_active,1,10) = '$_POST[code]' ";
                            $r = $this->db->query($sql);
                            if( $r->num_rows() > 0){
                                $sql = "UPDATE  collegians_of_activity SET allow_activity = 'Y' , date_code_active  = NOW() 
                                            WHERE  collegians_of_activity.collegians_of_activity_id = $id  ";
                                $this->db->query($sql);

                                echo '<query>
                                             <status q="true">บันทึกรหัสเสร็จเรียบร้อยแล้ว</status>
                                        </query>';
                            }else{
                                echo '<query>
                                             <status q="false">รหัสลงทะเบียนไม่ถูกต้อง</status>
                                        </query>';
                            }
                           
                            
                        
                    }
                }
                
                public function collegian_activity_confirm(){
                    /* ยืนยันการเข้าร่วมกิจกรรมของนักศึกษา */
                    $this->load->model("activity_model");
                    if(isset($_POST["act_id"])){
                        /* โหลดข้อมูลนักศึกษา */
                        $collegian_datay = $this->collegian_model->get_collegian_by_id($_SESSION["id"]);
                        /* ถ้ามีนักศึกษา */
                        if($collegian_datay->collegian_id !=""){
                            /* เก็บชั้นปีของนักศึกษา */
                            $year_class = get_level($collegian_datay->collegian_code);
                            /* เก็บสาขาววิชานักศึกษา */
                            $is_major = $collegian_datay->ref_major_id;
                            /*  นับจำนวนสาขาวิชาที่เข้าร่วมกิจกรรมว่ามีอยู่ก่อนหน้า หรือไม่ */
                            $this->db->select("*",false)->from("group_major_relation")
                                    ->join("activity_groups_relation","activity_groups_relation.activity_groups_relation_id = group_major_relation.ref_activity_groups_relation_id","inner");
                            $this->db->where("ref_major_id",$is_major); 
                            $this->db->where("year_class",$year_class);
                            $this->db->where("ref_act_id",$_POST["act_id"]);
                            $count_major_exists =  $this->db->get();
                            if($count_major_exists->num_rows()  == 0){ /* ถ้ายังไม่มีข้อมูลในตาราง ( group_major_relation ) เนื่องจากอาจเป็นคนแรกในสาขาที่ลงทะเบียน */
                                /* ดึงข้อมูลจากตาราง activity_groups_relation มาเก็บไว้เพื่ออ้างอิง*/
                            
                                $activity_groups_relation_datay = $this->activity_model->get_activity_groups_relation_by_activity_id($_POST["act_id"]);
                                
                                if( $activity_groups_relation_datay->num_rows() == 1){
                                      $sql = "INSERT  INTO  group_major_relation  VALUES(
                                            NULL , 
                                            '" . $activity_groups_relation_datay->row()->activity_groups_relation_id . "' ,
                                            '" .  $collegian_datay->major_id  ."' , 
                                            '" .  $year_class  .  "'
                                      )";
                                     
                                      $this->db->query($sql);
                                      $last_group_major_relation_id = $this->db->insert_id();
                                      
                                }
                            }else{
                                      $last_group_major_relation_id = $count_major_exists->row(0)->group_major_relation_id;
                                     
                            }
                            $code = md5(date("d-m-Y H:i:s") . $collegian_datay->collegian_id . rand(1, 5000));
                            $sql = "INSERT  INTO collegians_of_activity VALUES(
                                NULL , 
                                '{$last_group_major_relation_id}' , 
                                '{$collegian_datay->collegian_id} ' ,
                                '$code' , 
                                 'Y' ,
                                 'N' ,
                                 NULL
                            )";
                            $this->db->query($sql);
                         //     file_put_contents("tttt.txt", $sql);
                            
                        }
                        
                    }
                    
                }
               
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */